package com.gpframework.module.purchase.service.impl;

import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.module.domain.req.GpIncomingInspectUpdate;
import com.gpframework.module.purchase.domain.GpPurchaseIncomingInspect;
import com.gpframework.module.purchase.domain.req.GpPurchaseIncomingInspectUpdate;
import com.gpframework.module.purchase.service.IGpPurchaseIncomingInspectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.purchase.mapper.GpPurchaseQualityFeedbackMapper;
import com.gpframework.module.purchase.domain.GpPurchaseQualityFeedback;
import com.gpframework.module.purchase.domain.vo.GpPurchaseQualityFeedbackVo;
import com.gpframework.module.purchase.service.IGpPurchaseQualityFeedbackService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpPurchaseQualityFeedback
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 采购品质反馈Service业务层处理
 */
@Service
public class GpPurchaseQualityFeedbackServiceImpl extends ServiceImpl<GpPurchaseQualityFeedbackMapper, GpPurchaseQualityFeedback> implements IGpPurchaseQualityFeedbackService {

    @Autowired
    private IGpPurchaseIncomingInspectService iGpPurchaseIncomingInspectService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [lqw]
     * @Return List<GpPurchaseQualityFeedbackVo>
     */
    @Override
    public List<GpPurchaseQualityFeedbackVo> listVo(QueryWrapper<GpPurchaseQualityFeedback> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [id]
     * @Return GpPurchaseQualityFeedbackVo
     */
    @Override
    public GpPurchaseQualityFeedbackVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean addQualityfeedback(GpPurchaseIncomingInspectUpdate gpPurchaseIncomingInspectUpdate) {
        List<Long> ids = gpPurchaseIncomingInspectUpdate.getIds();
        GpPurchaseIncomingInspect gpPurchaseIncomingInspect;
        List<GpPurchaseQualityFeedback> list = new ArrayList<>();
        Date now = new Date();
        for (Long id : ids) {
            gpPurchaseIncomingInspect = iGpPurchaseIncomingInspectService.getById(id);
            if (gpPurchaseIncomingInspect == null) {
                continue;
            }
            GpPurchaseQualityFeedback gpPurchaseQualityFeedback = new GpPurchaseQualityFeedback();
            BeanUtils.copyProperties(gpPurchaseIncomingInspect,gpPurchaseQualityFeedback);
            if (StringUtils.isNotBlank(gpPurchaseIncomingInspectUpdate.getCreateBy())) {
                gpPurchaseQualityFeedback.setCreateBy(gpPurchaseIncomingInspectUpdate.getCreateBy());
            }
            gpPurchaseQualityFeedback.setIncomingInspectId(gpPurchaseIncomingInspect.getId());
            gpPurchaseQualityFeedback.setFeedbackId(gpPurchaseIncomingInspectUpdate.getFeedbackId());
            gpPurchaseQualityFeedback.setFeedbackName(gpPurchaseIncomingInspectUpdate.getFeedbackName());
            gpPurchaseQualityFeedback.setFeedbackType(gpPurchaseIncomingInspectUpdate.getFeedbackType());
            gpPurchaseQualityFeedback.setFeedbackContent(gpPurchaseIncomingInspectUpdate.getFeedbackContent());
            gpPurchaseQualityFeedback.setFeedbackTime(now);
            list.add(gpPurchaseQualityFeedback);
        }
        return this.saveBatch(list);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpPurchaseQualityFeedback gpPurchaseQualityFeedback = this.getById(id);
        if (gpPurchaseQualityFeedback == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpPurchaseQualityFeedback.setId(null);
            this.save(gpPurchaseQualityFeedback);
        }
        return true;
    }
}
